package org.autumn.lock;

import java.util.concurrent.TimeUnit;

/**
 * 定位死锁
 * jps
 *
 * jstack 50552
 *
 * jconsole
 *
 *
 * "t2" #11 prio=5 os_prio=31 tid=0x00007fecb508c800 nid=0x3f03 waiting for monitor entry [0x0000700009672000]
 *    java.lang.Thread.State: BLOCKED (on object monitor)
 *         at org.autumn.lock.DieLock.lambda$main$1(DieLock.java:43)
 *         - waiting to lock <0x000000076ac32df0> (a java.lang.Object)
 *         - locked <0x000000076ac32e00> (a java.lang.Object)
 *         at org.autumn.lock.DieLock$$Lambda$2/1072408673.run(Unknown Source)
 *         at java.lang.Thread.run(Thread.java:750)
 *
 * "t1" #10 prio=5 os_prio=31 tid=0x00007fecb491b800 nid=0x3803 waiting for monitor entry [0x000070000956f000]
 *    java.lang.Thread.State: BLOCKED (on object monitor)
 *         at org.autumn.lock.DieLock.lambda$main$0(DieLock.java:28)
 *         - waiting to lock <0x000000076ac32e00> (a java.lang.Object)
 *         - locked <0x000000076ac32df0> (a java.lang.Object)
 *         at org.autumn.lock.DieLock$$Lambda$1/2065951873.run(Unknown Source)
 *         at java.lang.Thread.run(Thread.java:750)
 *
 *
 * "t2":
 *   waiting to lock monitor 0x00007fecb48148a8 (object 0x000000076ac32df0, a java.lang.Object),
 *   which is held by "t1"
 * "t1":
 *   waiting to lock monitor 0x00007fecb4812018 (object 0x000000076ac32e00, a java.lang.Object),
 *   which is held by "t2"
 *
 *
 *   "t2":
 *         at org.autumn.lock.DieLock.lambda$main$1(DieLock.java:43)
 *         - waiting to lock <0x000000076ac32df0> (a java.lang.Object)
 *         - locked <0x000000076ac32e00> (a java.lang.Object)
 *         at org.autumn.lock.DieLock$$Lambda$2/1072408673.run(Unknown Source)
 *         at java.lang.Thread.run(Thread.java:750)
 * "t1":
 *         at org.autumn.lock.DieLock.lambda$main$0(DieLock.java:28)
 *         - waiting to lock <0x000000076ac32e00> (a java.lang.Object)
 *         - locked <0x000000076ac32df0> (a java.lang.Object)
 *         at org.autumn.lock.DieLock$$Lambda$1/2065951873.run(Unknown Source)
 *         at java.lang.Thread.run(Thread.java:750)
 */
public class DieLock {
    private static Object l1 = new Object();
    private static Object l2 = new Object();

    public static void main(String[] args) {
        // 死锁
        new Thread(() -> {
            synchronized (l1) {
                System.out.println("lock l1");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (l2){
                    System.out.println("lock l2");
                }
            }
        }, "t1").start();


        new Thread(() -> {
            synchronized (l2) {
                System.out.println("lock l2");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (l1){
                    System.out.println("lock l1");
                }
            }
        }, "t2").start();
    }
}
